﻿#include "pmbus_parse.h"
#include <boost/algorithm/hex.hpp>
#include <boost/algorithm/string.hpp>


using namespace std;

/*****************************************************************************
 Prototype    : pmbus_package_cmd
 Description  : 发送的电总协议打包
 Input        : U8 ver----协议版本号
                U8 addr---485地址
                U8 cid1---命令字1
                U8 cid2---命令字2,
                
 Output       : msg---打包好的数据
 Return Value : void

  History        :
  1.Date         : 2018/7/17
    Author       : wuquanwei
    Modification : Created function

*****************************************************************************/
void pmbus_package_cmd(uint8_t ver, uint8_t addr, uint8_t cid1, uint8_t cid2, string &msg, const string &data)
{
	unsigned short len = sizeof(PmbusMsgHeadSt) +data.size()+3; //加上校验和一个结束符
	
	PmbusMsgHeadSt *pstMsg = (PmbusMsgHeadSt *) new char [len]; //查询指令的长度是固定的
    uint16_t infoLen = data.size();
	
	pstMsg->packFlag = 0x7E;
	pstMsg->ver  = ver;
	pstMsg->addr = addr;
	pstMsg->length[0] = (infoLen*2) >> 8 ; //info字段数据长度的2倍，高在前，低在后
	pstMsg->length[1] = (infoLen*2) & 0xff;   //

	pstMsg->length[0] |=  kstar_length_sumCheck(pstMsg->length);
	pstMsg->cid1 = cid1;
	pstMsg->cid2 = cid2;

	if ( data.size() != 0 )
	{
		memcpy(pstMsg->info, data.c_str(), data.size());
	}
	
	string msgData=boost::algorithm::hex(std::string((char *)&pstMsg->ver, len-4)); //校验是从ver开始，到检验前一个字节结束 
	
    unsigned short checkSum = kstar_msg_sumCheck((uint8_t*)msgData.c_str(), msgData.size());

	msg = "~";
	msg += msgData;
	char sum[2] = {0};
	
	sum[0] = checkSum >> 8;
	sum[1] = checkSum & 0xff;
	string check=boost::algorithm::hex(std::string((char *)sum, 2));


	msg += check;
	msg += "\x0D";
		

	delete [] pstMsg;
	
}


#if  0

/*****************************************************************************
 Prototype    : pmbus_package_cmd
 Description  : 
 Input        : statAddr----开始地址
                const string & data--数据字符串
                struct device *dev---设备信息
                queue<struct note> *pnote 输出信息,
                
 Output       : queue<struct note> *pnote 输出信息,
 Return Value : void

  History        :
  1.Date         : 2018/7/17
    Author       : wuquanwei
    Modification : Created function

*****************************************************************************/
void process_strlist_value(int statAddr, const string & data, struct device *dev,queue<struct note> *pnote)
{
#ifdef _TEST_
	debug_print((const char * )data.c_str(),  data.size());
#endif

	string tmpStr;
	map<int, PasscodeValInfoSt *>::iterator iter;

	std::vector<std::string> SplitVec;
	boost::split(SplitVec, data, boost::is_any_of(" "), boost::token_compress_off);

	int size = SplitVec.size();
	for ( int i = 0; i < size; i++ )
	{
		iter = m_modbusAddrMap.find(statAddr+i);
		if ( iter != m_modbusAddrMap.end() )
		{
#ifdef _TEST_
			printf("libitrust_ups, addr: %d, passcode: %d\n",statAddr+i,iter->second->passcode);
#endif
			tmpStr = SplitVec[i];
			if( tmpStr.find("---") != string::npos )
			{
				tmpStr = "0";
			}
			
			if ( String == iter->second->dataType )
			{
				modbus_process_string_value(iter->second, SplitVec[i], dev, pnote);
			}
			else if (ByteArray == iter->second->dataType)
			{
				process_key_value(statAddr+i, SplitVec[i].c_str(), SplitVec[i].size(), dev, pnote);
			}
		}
	}
	
}


#endif


























